package com.tac.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tac.common.Exception.CosTomException;
import com.tac.common.result.Result;
import com.tac.dao.OccupantDao;
import com.tac.pojo.Occupant;
import com.tac.pojo.vo.OccupantVo;
import com.tac.service.OccupantService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OccupantServiceImpl extends ServiceImpl<OccupantDao, Occupant> implements OccupantService {

    /**
     * 查看用户的常用入住人名字
     * @param uid
     * @return
     */
    public Result<List<String>> selectName(Long uid){
        LambdaQueryWrapper<Occupant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Occupant::getName);
        queryWrapper.eq(Occupant::getUserId,uid);
        List<Occupant> list = this.list(queryWrapper);
        ArrayList<String> arrayList = new ArrayList<>();
        for(Occupant occupant:list){
            String name = occupant.getName();
            arrayList.add(name);
        }
        return Result.success(arrayList,"success");
    }

    /**
     * 查看用户的常用入住人和数量
     * @param uid
     * @return
     */
    public Result<OccupantVo> selectAll(Long uid){
        Occupant occupant = new Occupant();
        occupant.setUserId(uid);
        List<Occupant> list = this.list(Wrappers.query(occupant));
        OccupantVo occupantVo = new OccupantVo();
        occupantVo.setOccupants(list);
        int num = this.count(Wrappers.query(occupant));
        occupantVo.setNum(num);
        return Result.success(occupantVo,"success");
    }

    /**
     * 添加常用入住人
     * @param occupant
     * @param uid
     * @return
     */
    public Result inserte(Occupant occupant,Long uid){
        Occupant occupant1 = new Occupant();
        occupant1.setUserId(uid);
        occupant1.setName(occupant.getName());
        occupant1.setIdcard(occupant.getIdcard());
        LambdaQueryWrapper<Occupant> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Occupant::getId,uid);
        int count = this.count(queryWrapper);
        if(count>=10){
            throw  new CosTomException("最多只能存十个常用入住人");
        }
        boolean save = this.save(occupant1);
        if(!save){
            throw  new CosTomException("添加异常");
        }
        return Result.success(null,"success",201);
    }

    /**
     * 删除常用入住人
     * @param id
     * @return
     */
    public Result removeByOccupant(Long id){
        boolean b = this.removeById(id);
        if(!b){
            throw  new CosTomException("添加失败");
        }
        return Result.success(null,"success",204);
    }

    /**
     * 更新常用入住人
     * @param occupant
     * @return
     */
    public Result updateByOccupant(Occupant occupant){
        boolean b = this.updateById(occupant);
        if(!b){
            throw  new CosTomException("更新异常");
        }
        return Result.success(null,"success");
    }

}
